<?php
if (!defined('IS_ADMIN'))
	die('Error 404 : Not found.');

$condition = '';
$order = 'g_id DESC';
$url = '?mod=games';

// For search games
if ($keyword) {
	$condition = ' WHERE g_name LIKE "%' . $keyword . '%"';
	$order = 'g_name ASC';
	$url = '?mod=games&keyword=' . $keyword;
}
// Popular games
if ($popular == 1) {
	$condition = ' WHERE popular = 1';
	$url = '?mod=games&popular=1';
}
// Games is suspended
if ($active == 0) {
	$condition = ' WHERE active = 0';
	$url = '?mod=games&active=0';
}

// total number of games
$Sql = 'SELECT g_id
		FROM games' . $condition;
$total = $db->numRows($Sql);

if ($total > $limit) {
	$panigation = ad_panigation($total, $limit, $page, $url, 4, true);
	$smarty->assign('panigation', $panigation);
}

// list table of games
$Qry = 'SELECT *
		FROM games' . $condition . '
		ORDER BY ' . $order . '
		LIMIT ' . $start . ',' . $limit;
$games = $db->fetchAll($Qry);

$listAll = array();
foreach ($games as $key=>$value) {
	// Categories
	$category = explode(',', $value['category']);
	$cats = '';
	foreach ($category as $k=>$cat) {
		$cats .= '<a href="' . control_cat . strtolower($cat) . '" target="_blank">' . ucfirst($cat) . '</a>, ';
	}
	$cats = rtrim($cats, ', ');
	$value[category] = $cats;

	// Tags
	$alltags = explode(',', $value['tags']);
	$tags = '';
	foreach ($alltags as $k=>$tag) {
		$tags .= '<a href="' . control_tag . strtolower($tag) . '" target="_blank">' . ucfirst($tag) . '</a>, ';
	}
	$tags = rtrim($tags, ', ');
	$value[tags] = $tags;

	// Images
	if ($value['images']) {
		$img_path = DC_ROOT . '/data/uploads/' . $value['images'];

		if (file_exists($img_path))
			$value['images'] = '<img src="/img.php?src=/data/uploads/' . $value['images'] . '&w=75&h=55" width="75" height="55" alt="img" />';
	}

	// Dump
	$listAll[] = $value;
}
// assign
$smarty->assign('games', $listAll);
// SEO
$smarty->assign('title', 'Games - ' . $lang['home_title']);
// Menu
$smarty->assign('is_menu', 'games');
// display theme using smarty
$smarty->display(str_replace(SKIN, 'admin', SMARTY_TEMP) . 'games.tpl');